﻿
using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

using System.IO;
using Microsoft.Xna.Framework.Graphics;


namespace DS.SludgeEngine.Base.GlobalContent
{
    public sealed class GlobalContent
    {
        public static Dictionary<string, Effect> Effects
        {
            get;
            set;
        }
        public static Dictionary<string, Model> Models
        {
            get;
            set;
        }

        public static Dictionary<string, Texture2D> Texture2Ds
        {
            get;
            set;
        }
        public static Dictionary<string, SpriteFont> Fonts
        {
            get;
            set;
        }

        public static ContentManager Manager
        {
            get;
            set;
        }

        public static ContentConfiguration Configuration
        {
            get;
            set;
        }

        public static int LoadingCount
        {
            get;
            private set;
        }

        public static int ContentCount
        {
            get;
            private set;
        }

        public static string CurrentFile
        {
            get;
            private set;
        }

        public static string CurrentContentType
        {
            get;
            private set;
        }

        private static List<ContentItem> itemsToLoad = new List<ContentItem>();
        
        public static void InitialiseContent(string contentXml)
        {
            string xml = File.ReadAllText(contentXml);
            Configuration = ContentConfiguration.Deserialise(xml);
            Effects = new Dictionary<string, Effect>();
            Models = new Dictionary<string, Model>();
            Texture2Ds = new Dictionary<string, Texture2D>();
            Fonts = new Dictionary<string, SpriteFont>();
        }
        public static void PreLoadGroup(string groupName)
        {
            LoadingCount = 0;
            itemsToLoad.Clear();
            LoadItemType(groupName, "Effect");
            LoadItemType(groupName, "Model");
            LoadItemType(groupName, "Texture2D");
            LoadItemType(groupName, "Font");
            ContentCount = itemsToLoad.Count;



        }
        public static void LoadNextItem()
        {
            LoadItem(itemsToLoad[LoadingCount]);
            LoadingCount++;
        }

        public static void LoadAll(string groupName)
        {
            LoadingCount = 0;
            itemsToLoad.Clear();
            LoadItemType(groupName, "Effect");
            LoadItemType(groupName, "Model");
            LoadItemType(groupName, "Texture2D");
            LoadItemType(groupName, "Font");
            foreach (ContentItem item in itemsToLoad)
            {
                LoadItem(item);
            }

        }


        private static void LoadItemType(string groupName, string itemType)
        {
            var groupItem =
               (from n in Configuration.Groups
                where n.Name == groupName
                select n).First();

            var items =
               from n in groupItem.ContentItems
               where n.ContentType == itemType
               select n;

            itemsToLoad.AddRange(items.ToArray());

        }

        private static void LoadItem(ContentItem item)
        {
            
            switch (item.ContentType)
            {
                case "Effect":
                    CurrentContentType = "Effects";
                    Effect newEffect = GameInformation.Game.Content.Load<Effect>(item.FileLocation);
                    Effects.Add(item.ContentKey, newEffect);
                    break;
                case "Model":
                    CurrentContentType = "Models";
                    Model newModel = GameInformation.Game.Content.Load<Model>(item.FileLocation);
                    Models.Add(item.ContentKey, newModel);
                    break;
                case "Texture2D":
                    CurrentContentType = "2D Textures";
                    Texture2D newTexture = GameInformation.Game.Content.Load<Texture2D>(item.FileLocation);
                    Texture2Ds.Add(item.ContentKey, newTexture);
                    break;
                case "Font":
                    CurrentContentType = "Fonts";
                    SpriteFont newFont = GameInformation.Game.Content.Load<SpriteFont>(item.FileLocation);
                    Fonts.Add(item.ContentKey, newFont);
                    break;
                default:
                    break;
            }
            CurrentFile = item.FileLocation;
        }

        public static void UnloadGroup(string groupName)
        {
            UnloadEffects(groupName);
            UnloadModels(groupName);
            UnloadTextures(groupName);
            UnloadFonts(groupName);
        }


   
        private static void UnloadEffects(string groupName)
        {
            var groupItem =
               (from n in Configuration.Groups
                where n.Name == groupName
                select n).First();

            var items =
               from n in groupItem.ContentItems
               where n.ContentType == "Effect"
               select n;

            foreach (ContentItem item in items)
            {
                Effects[item.ContentKey].Dispose();
                Effects.Remove(item.ContentKey);

            }
        }
        private static void UnloadModels(string groupName)
        {
            var groupItem =
               (from n in Configuration.Groups
                where n.Name == groupName
                select n).First();

            var items =
               from n in groupItem.ContentItems
               where n.ContentType == "Model"
               select n;

            foreach (ContentItem item in items)
            {
                Models.Remove(item.ContentKey);
            }
        }
        private static void UnloadTextures(string groupName)
        {
            var groupItem =
               (from n in Configuration.Groups
                where n.Name == groupName
                select n).First();

            var items =
               from n in groupItem.ContentItems
               where n.ContentType == "Texture2D"
               select n;

            foreach (ContentItem item in items)
            {

                Texture2Ds[item.ContentKey].Dispose();
                Texture2Ds.Remove(item.ContentKey);

            }
        }
        private static void UnloadFonts(string groupName)
        {
            var groupItem =
               (from n in Configuration.Groups
                where n.Name == groupName
                select n).First();

            var items =
               from n in groupItem.ContentItems
               where n.ContentType == "Font"
               select n;

            foreach (ContentItem item in items)
            {

                Fonts.Remove(item.ContentKey);

            }
        }

    }
}
